package com.lncg.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 应用程序配置类
 * 简化版本，包含所有必要的配置
 */
@Component
@ConfigurationProperties(prefix = "")
public class AppConfig {
    
    // SQL Server配置
    private String sqlServerUrl;
    private String sqlServerUsername;
    private String sqlServerPassword;
    private String sqlServerDriver = "net.sourceforge.jtds.jdbc.Driver";
    
    // MySQL配置
    private String mysqlUrl;
    private String mysqlUsername;
    private String mysqlPassword;
    private String mysqlDriver = "com.mysql.cj.jdbc.Driver";
    
    // 新数据源配置 - SQL Server 2016 (NBIotDB)
    private String sqlServer2016Url;
    private String sqlServer2016Username;
    private String sqlServer2016Password;
    private String sqlServer2016Driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    
    // 同步配置
    private String syncCron = "0 0 8 * * ?"; // 默认每天8点执行
    private boolean syncEnabled = true;
    
    // Getters and Setters
    public String getSqlServerUrl() {
        return sqlServerUrl;
    }
    
    public void setSqlServerUrl(String sqlServerUrl) {
        this.sqlServerUrl = sqlServerUrl;
    }
    
    public String getSqlServerUsername() {
        return sqlServerUsername;
    }
    
    public void setSqlServerUsername(String sqlServerUsername) {
        this.sqlServerUsername = sqlServerUsername;
    }
    
    public String getSqlServerPassword() {
        return sqlServerPassword;
    }
    
    public void setSqlServerPassword(String sqlServerPassword) {
        this.sqlServerPassword = sqlServerPassword;
    }
    
    public String getSqlServerDriver() {
        return sqlServerDriver;
    }
    
    public void setSqlServerDriver(String sqlServerDriver) {
        this.sqlServerDriver = sqlServerDriver;
    }
    
    public String getMysqlUrl() {
        return mysqlUrl;
    }
    
    public void setMysqlUrl(String mysqlUrl) {
        this.mysqlUrl = mysqlUrl;
    }
    
    public String getMysqlUsername() {
        return mysqlUsername;
    }
    
    public void setMysqlUsername(String mysqlUsername) {
        this.mysqlUsername = mysqlUsername;
    }
    
    public String getMysqlPassword() {
        return mysqlPassword;
    }
    
    public void setMysqlPassword(String mysqlPassword) {
        this.mysqlPassword = mysqlPassword;
    }
    
    public String getMysqlDriver() {
        return mysqlDriver;
    }
    
    public void setMysqlDriver(String mysqlDriver) {
        this.mysqlDriver = mysqlDriver;
    }
    
    public String getSyncCron() {
        return syncCron;
    }
    
    public void setSyncCron(String syncCron) {
        this.syncCron = syncCron;
    }
    
    public boolean isSyncEnabled() {
        return syncEnabled;
    }
    
    public void setSyncEnabled(boolean syncEnabled) {
        this.syncEnabled = syncEnabled;
    }
    
    // 新数据源配置的Getters and Setters
    public String getSqlServer2016Url() {
        return sqlServer2016Url;
    }
    
    public void setSqlServer2016Url(String sqlServer2016Url) {
        this.sqlServer2016Url = sqlServer2016Url;
    }
    
    public String getSqlServer2016Username() {
        return sqlServer2016Username;
    }
    
    public void setSqlServer2016Username(String sqlServer2016Username) {
        this.sqlServer2016Username = sqlServer2016Username;
    }
    
    public String getSqlServer2016Password() {
        return sqlServer2016Password;
    }
    
    public void setSqlServer2016Password(String sqlServer2016Password) {
        this.sqlServer2016Password = sqlServer2016Password;
    }
    
    public String getSqlServer2016Driver() {
        return sqlServer2016Driver;
    }
    
    public void setSqlServer2016Driver(String sqlServer2016Driver) {
        this.sqlServer2016Driver = sqlServer2016Driver;
    }
    
    /**
     * 验证配置是否完整
     */
    public void validateConfig() {
        StringBuilder errors = new StringBuilder();
        
        if (sqlServerUrl == null || sqlServerUrl.trim().isEmpty()) {
            errors.append("❌ SQL Server URL未配置\n");
        }
        if (sqlServerUsername == null || sqlServerUsername.trim().isEmpty()) {
            errors.append("❌ SQL Server用户名未配置\n");
        }
        if (sqlServerPassword == null || sqlServerPassword.trim().isEmpty()) {
            errors.append("❌ SQL Server密码未配置\n");
        }
        if (mysqlUrl == null || mysqlUrl.trim().isEmpty()) {
            errors.append("❌ MySQL URL未配置\n");
        }
        if (mysqlUsername == null || mysqlUsername.trim().isEmpty()) {
            errors.append("❌ MySQL用户名未配置\n");
        }
        if (mysqlPassword == null || mysqlPassword.trim().isEmpty()) {
            errors.append("❌ MySQL密码未配置\n");
        }
        if (sqlServer2016Url == null || sqlServer2016Url.trim().isEmpty()) {
            errors.append("❌ SQL Server 2016 URL未配置\n");
        }
        if (sqlServer2016Username == null || sqlServer2016Username.trim().isEmpty()) {
            errors.append("❌ SQL Server 2016用户名未配置\n");
        }
        if (sqlServer2016Password == null || sqlServer2016Password.trim().isEmpty()) {
            errors.append("❌ SQL Server 2016密码未配置\n");
        }
        
        if (errors.length() > 0) {
            System.err.println("=== 配置错误 ===");
            System.err.println(errors.toString());
            System.err.println("请检查 application.yml 配置文件！");
            throw new RuntimeException("配置验证失败: " + errors.toString());
        }
        
        System.out.println("✅ 配置验证通过");
    }
}
